package com.mdd.admin.controller.coupon;

import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.coupon.ICouponService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.coupon.CouponShareValidate;
import com.mdd.admin.validate.coupon.CouponValidate;
import com.mdd.admin.vo.coupon.CouponListCountVo;
import com.mdd.admin.vo.coupon.CouponListVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/coupon")
@Api(tags = "优惠券管理")
public class CouponController {

    @Resource
    private ICouponService iCouponService;

    /**
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    @ApiOperation(value = "优惠券管理 - 列表")
    public AjaxResult list(@Validated PageParam pageParam,
                           @RequestParam Map<String, String> params){
        PageResult<CouponListVo> list = iCouponService.list(pageParam, params);
        return AjaxResult.success(list);
    }

    @GetMapping("/listCount")
    @ApiOperation(value = "优惠券管理 - 列表统计")
    public AjaxResult countList() {
        CouponListCountVo couponListCountVo = iCouponService.countList();
        return AjaxResult.success(couponListCountVo);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "优惠券管理 - 优惠券详情")
    public AjaxResult detail(@RequestParam("id") Integer id) {
        return AjaxResult.success(iCouponService.detail(id));
    }

    @Log(title = "新增优惠券")
    @PostMapping("/create")
    @ApiOperation(value = "优惠券管理 - 新增")
    public AjaxResult create(@Validated(value = CouponValidate.create.class)
                             @RequestBody CouponValidate couponValidate){
        iCouponService.create(couponValidate);
        return AjaxResult.success();
    }

    @Log(title = "编辑优惠券")
    @PostMapping("/edit")
    @ApiOperation(value = "优惠券管理 - 编辑")
    public AjaxResult edit(@Validated(value = CouponValidate.update.class)
                             @RequestBody CouponValidate couponValidate){
        iCouponService.edit(couponValidate);
        return AjaxResult.success();
    }

    @Log(title = "删除优惠券")
    @PostMapping("/delete")
    @ApiOperation(value = "优惠券管理 - 删除")
    public AjaxResult delete(@Validated(value = CouponValidate.delete.class)
                             @RequestBody CouponValidate couponValidate){
        iCouponService.delete(couponValidate.getId());
        return AjaxResult.success();
    }

    @Log(title = "开始优惠券")
    @GetMapping("/start")
    @ApiOperation(value = "优惠券管理 - 开始优惠券活动")
    public AjaxResult start(@Validated @IDMust(message = "id必须存在且大于等于0")
                            @RequestParam Integer id){
        iCouponService.start(id);
        return AjaxResult.success();
    }

    @Log(title = "停止优惠券")
    @GetMapping("/stop")
    @ApiOperation(value = "优惠券管理 - 停止优惠券活动")
    public AjaxResult stop(@Validated @IDMust(message = "id必须存在且大于等于0")
                            @RequestParam Integer id){
        iCouponService.stop(id);
        return AjaxResult.success();
    }

    @Log(title = "获取分享二维码")
    @GetMapping("/getCouponImage")
    @ApiOperation(value = "优惠券管理 - 获取分享二维码")
    public AjaxResult getCouponImage(@Validated @IDMust(message = "id必须存在且大于等于0")
                           @RequestParam Integer id){
        return AjaxResult.success("成功获取", iCouponService.getCouponImage(id));
    }

    /**
     * @param id 优惠券id
     * @param userIds  用户id
     * @return Object
     */
    @Log(title = "添加和修改优惠券分享")
    @PostMapping("/share")
    @ApiOperation(value = "优惠券管理 - 优惠券分享")
    public AjaxResult share(@Validated @IDMust(message = "id必须存在且大于等于0")
                            @RequestParam Integer id, @RequestBody List<CouponShareValidate> userIds){
        iCouponService.share(id, userIds);
        return AjaxResult.success();
    }

    @Log(title = "优惠券分享列表查询")
    @GetMapping("/shareList")
    @ApiOperation(value = "优惠券管理 - 优惠券分享列表")
    public AjaxResult shareList(@RequestParam Map<String, String> params){
        return AjaxResult.success(iCouponService.shareList(params));
    }
}
